package com.uottawa.library.service;

import java.util.List;

import com.uottawa.library.dao.UserDAO;
import com.uottawa.library.domain.User;

public class UserService {
	
	private UserDAO dao = new UserDAO();
	
	public String deleteUser(User user) { // should be the admin only
		dao.updateUserBalance();
		double balance = dao.getUserBalance(user.getUserId());
		if(balance > 0) {
			return "failed, outstanding balance";
		}
		int activeLoansCount = dao.getActiveLoans(user);
		
		if(activeLoansCount > 0) {
			return "failed, user has active loans";
		}
		else
		{
		dao.removeUser(user);
		return "success";
		}
	}

	public String addUser(User user) {
		boolean added = dao.addUser(user);
		if(added) {
			return "success";
		}
		return "failed, duplicate entry";
	}

	public User login(String userName, String password) {
		User user = dao.getUser(userName, password);
		return user;
	}

	public User getUser(int userId) {
		return dao.getUser(userId);
	}
	
	public List<User> getAllUser()
	{
		return dao.getAllUser();
	}

	public String deleteUser(int userId) {
		double balance = dao.getUserBalance(userId);
		if(balance > 0) {
			return "failed, outstanding balance";
		}
		int activeLoansCount = dao.getActiveLoans(userId);
		
		if(activeLoansCount > 0) {
			return "failed, user has active loans";
		}
		else
		{
		return (dao.removeUser(userId)) ? "User has been deleted": "user is not deleted";
		}
	}

}
